import fetchAsync from './async';

export default {

  state: {
    value: 0,
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      console.log('one subscriptions setup run');
    },
  },

  effects: {
    *increment({ payload }, { select, call, put }) {  // eslint-disable-line
      console.log('one effects increment run');
      const state = (yield select(state => state.value)) + 1;
      const value = yield call(fetchAsync, { state });
      yield put({ type: 'incrementAfter', payload });
      yield put({ type: 'save', payload: { value } });
    },
    *incrementAfter({ payload }, { call }) {
      const msg = yield call(() => new Promise((resolve, reject) => {
        setTimeout(resolve, 1000, 'incrementAfter run end!');
      }));
      console.log(msg);
    },
    *decrement({ payload }, { select, call, put }) {  // eslint-disable-line
      console.log('one effects decrement run');
      const state = (yield select(state => state.value)) - 1;
      const value = yield call(fetchAsync, { state });
      yield put({ type: 'save', payload: { value } });
    },
  },

  reducers: {
    save(state, action) {
      console.log('one reducers save run');
      return { ...state, ...action.payload };
    },
  },
}
